const wx2my = require('../../wx2my');

const app = getApp();
let page = 1;
let totalpage;
Page({
  data: {
    money: "",
    onOff: 0
  },
  onLoad: function (e) {},
  onReady: function () {},
  onShow: function () {
    page = 1;
    this.get_list();
    this.get_msg();
  },

  all() {
    this.setData({
      txmoney: this.data.money
    });
  },

  moneyChange(e) {
    console.log(e);
    let that = this;
    let txmoney = e.detail.value;
    this.setData({
      txmoney: txmoney
    });
  },

  handle_tx() {
    if (this.data.onOff) return false;

    if (app.fm.isNull(this.data.money)) {
      app.toast('请输入提现金额');
    } else {
      this.setData({
        onOff: 1
      });
      let that = this;
      app.wxRequest('withdraWal', {
        'shop_token': app.fm.gain('shop_token'),
        'money': that.data.txmoney
      }, function (res) {
        console.log(res);

        if (res.data.code == 1) {
          setTimeout(() => {
            wx2my.navigateBack();
          }, 2000);
        } else {
          that.setData({
            onOff: 0
          });
        }

        app.toast(res.data.msg);
      });
    }
  },

  onReachBottom() {
    page++;

    if (!this.data.lock) {
      this.get_list();
    }
  },

  get_msg() {
    let that = this;
    app.wxRequest('get_shop_money', {
      'shop_token': wx2my.getStorageSync('shop_token')
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let money = res.data.data.money;
        that.setData({
          money: money
        });
      }
    });
  },

  get_list() {
    let that = this;
    wx2my.showLoading({
      title: '加载中..'
    });
    app.wxRequest('withdrawLog', {
      'shop_token': app.fm.gain('shop_token'),
      'page': page
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let list = res.data.data.list;

        if (page == 1) {
          totalpage = res.data.data.totalpage;
        }

        that.setData({
          list: page == 1 ? list : that.data.list.concat(list),
          lock: totalpage <= page ? true : false
        });
      } else {
        app.toast(res.data.msg);
      }

      wx2my.hideLoading();
    });
  },

  onPullDownRefresh() {
    //下拉刷新
    let that = this;
    wx2my.showNavigationBarLoading();
    page = 1;
    app.wxRequest('withdrawLog', {
      'shop_token': app.fm.gain('shop_token'),
      'page': page
    }, function (res) {
      if (res.data.code == 1) {
        console.log(res);
        let list = res.data.data.list;
        that.setData({
          list: list,
          lock: totalpage <= page ? true : false
        });
        wx2my.hideNavigationBarLoading();
        wx2my.stopPullDownRefresh();
      } else {
        app.toast(res.data.msg);
      }
    });
  }

});